﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RealDolmen.Helpers
{
    public class JavascriptHelper
    {
        public static string ListToJsArray(List<string> arrayString)
        {
            List<string> newArrayString = arrayString.ConvertAll<string>(delegate(string old)
            {
                return string.Format("'{0}'", old);
            });

            return string.Join(",", newArrayString.ToArray());
        }

        public static string EscapeChar(string text, char charToEscape)
        {
            if (!string.IsNullOrEmpty(text))
            {
                StringBuilder reverse = new StringBuilder();

                bool charFound = false;
                for (int i = text.Length - 1; i >= 0; i--)
                {
                    char c = text[i];

                    reverse.Append(c);

                    if (c == charToEscape || (charFound && c == '\\'))
                    {
                        charFound = true;
                        reverse.Append('\\');
                    }
                    else
                        charFound = false;
                }
                text = reverse.ToString();
                reverse = new StringBuilder();
                for (int i = text.Length - 1; i >= 0; i--)
                {
                    reverse.Append(text[i]);
                }
                return reverse.ToString();
            }
            return string.Empty;
        }
    }
}
